/**
 * @author 徐楠
 * @date 2022/2/16 11:07
 * @version 1.0
 */

package com.xunan.likou;

import java.util.HashMap;
import java.util.Map;

public class RomanNumeralToInteger {
    public static void main(String[] args) {

        //String s = "III";
        //String s = "LVIII";
        String s = "MCMXCIV";//1994
        int result = romanToInt(s);
        System.out.println(result);

    }

    public static int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>();
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);
        System.out.println(map);

        int result = 0;
        for (int i = 0; i < s.length(); ++i) {
            int value = map.get(s.charAt(i));
            if (i < s.length() - 1 && value < map.get(s.charAt(i + 1))) {
                result -= value;
            } else {
                result += value;
            }
        }
        return result;

    }
}
